import numpy as np
import pandas as pd
from sklearn.preprocessing import StandardScaler, MinMaxScaler, RobustScaler
from sklearn.model_selection import train_test_split

# 读取数据文件
data_path = '/home/liudd/data_preprocessing/match_time201902_allband_L1_10.26.csv'
df = pd.read_csv(data_path, low_memory=False)  # 避免 DtypeWarning

df = df[df['fy_cth'] - df['cloudsat_cbh'] > 0]

# 划分训练集、验证集和测试集
train_df, test_df = train_test_split(df, test_size=0.2, random_state=42)
train_df, val_df = train_test_split(train_df, test_size=0.25, random_state=42)

def process_data(data):
    # 1. 对 fy_cth 进行归一化处理（MinMaxScaler）
    scaler_cth = MinMaxScaler()
    data['fy_cth_scaled'] = scaler_cth.fit_transform(data[['fy_cth']])

    # 2. 对 fy_ctt 进行归一化处理（MinMaxScaler）
    scaler_ctt = MinMaxScaler()
    data['fy_ctt_scaled'] = scaler_ctt.fit_transform(data[['fy_ctt']])

    # 3. 对 fy_ctp 进行对数变换
    data['fy_ctp_log'] = np.log1p(data['fy_ctp'])

    # 4. 对 fy_olr 进行稳健缩放处理（RobustScaler）
    scaler_olr = MinMaxScaler()
    data['fy_olr_scaled'] = scaler_olr.fit_transform(data[['fy_olr']])

    # 5. 处理 fy 的经纬度特征，映射为周期性特征（正弦和余弦变换）
    data['fy_lat_sin'] = np.sin(np.radians(data['fy_lat']))
    data['fy_lat_cos'] = np.cos(np.radians(data['fy_lat']))
    data['fy_lon_sin'] = np.sin(np.radians(data['fy_lon']))
    data['fy_lon_cos'] = np.cos(np.radians(data['fy_lon']))

    # 6、band1-6进行标准化，band7-14计算亮温并标准化

    # 亮温计算参数
    C1 = 1.19104 * 10 ** 8  # W·μm⁴·m⁻²·sr⁻¹
    C2 = 1.43877 * 10 ** 4  # μm·K

    # 确保这些列名在 DataFrame 中
    band_columns = [f'band{i}' for i in range(1, 15)]
    missing_bands = [col for col in band_columns if col not in data.columns]

    if missing_bands:
        print(f"缺少以下列: {missing_bands}")
    else:
        # 对 Band1 - Band6 直接进行标准化处理
        for band_num in range(1, 7):
            band_name = f'band{band_num}'
            scaler = StandardScaler()
            data[f'{band_name}_scaled'] = scaler.fit_transform(data[[band_name]])

        # 对 Band7 - Band14 计算亮温后进行标准化处理
        for band_num in range(7, 15):
            band_name = f'band{band_num}'
            center_wavelength = None
            if band_num == 7:
                center_wavelength = 3.75  # 根据实际情况区分高分辨率部分的中心波长
            elif band_num == 8:
                center_wavelength = 3.75  # 根据实际情况区分低分辨率部分的中心波长
            elif band_num == 9:
                center_wavelength = 6.25
            elif band_num == 10:
                center_wavelength = 7.1
            elif band_num == 11:
                center_wavelength = 8.5
            elif band_num == 12:
                center_wavelength = 10.8
            elif band_num == 13:
                center_wavelength = 12.0
            elif band_num == 14:
                center_wavelength = 13.5

            # 计算亮温
            radiation_brightness = data[band_name]
            data[band_name + '_brightness_temperature'] = C2 / (center_wavelength * np.log(1 + C1 / (radiation_brightness * center_wavelength**3)))

            scaler = StandardScaler()
            data[f'{band_name}_scaled'] = scaler.fit_transform(data[[band_name + '_brightness_temperature']])

    # 计算各个通道的灰度值
    for band_num in range(1, 15):
        band_name = f'band{band_num}'
        L = data[band_name]
        Lmin = data[band_name].min()
        Lmax = data[band_name].max()
        data[f'{band_name}_gray_value'] = ((L - Lmin) * 255) / (Lmax - Lmin)
    # 对灰度值进行标准化处理
    for band_num in range(1, 15):
        gray_value_col = f'band{band_num}_gray_value'
        scaler = StandardScaler()
        data[gray_value_col + '_scaled'] = scaler.fit_transform(data[[gray_value_col]])

    return data

# 处理训练集
train_df_processed = process_data(train_df)

# 处理验证集
val_df_processed = process_data(val_df)

# 处理测试集
test_df_processed = process_data(test_df)

# 保存为 CSV 文件
train_df_processed.to_csv('processed_train.csv', index=False)
val_df_processed.to_csv('processed_validation.csv', index=False)
test_df_processed.to_csv('processed_test.csv', index=False)